package com.songcha.module_task.ui.adapter

import android.graphics.Color
import android.text.SpannableStringBuilder
import android.text.Spanned
import android.text.style.ForegroundColorSpan
import android.widget.TextView
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.songcha.library_business.proxy.database.BookTodayCommentWriteCountDaoManager
import com.songcha.library_business.proxy.database.BookTodayReadChapterCountDaoManager
import com.songcha.library_business.proxy.database.BookTodayReadTimeDaoManager
import com.songcha.library_business.proxy.database.TodayWatchRewardAdvertCountDaoManager
import com.songcha.library_common.ui.view.CustomButton
import com.songcha.module_task.R
import com.songcha.module_task.bean.DailyTaskBean

class DailyTaskAdapter(list:MutableList<DailyTaskBean.DataBean>):BaseQuickAdapter<DailyTaskBean.DataBean,BaseViewHolder>(R.layout.task_item_daily_task,list) {
    companion object{
        val TYPE_READ_CHAPTER = 1
        val TYPE_READ_TIME = 2
        val TYPE_WRITE_COMMENT = 3
        val TYPE_WATCH_REWARD_ADVERT = 4
    }

    init {
        addChildClickViewIds(R.id.task_btn_goto)
    }

    override fun convert(holder: BaseViewHolder, item: DailyTaskBean.DataBean) {
        holder.getView<TextView>(R.id.task_tv_title).apply {
            text=item.everydayTaskTitle
        }

        holder.getView<TextView>(R.id.task_tv_desc).apply {
            val ssb=SpannableStringBuilder()
            ssb.append("您可获得+${item.awardGoldNumber}金币奖励")
            val colorSpan=ForegroundColorSpan(Color.parseColor("#FD4362"))
            ssb.setSpan(colorSpan,4,4+item.awardGoldNumber.toString().length+3,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
            text=ssb
        }

        holder.getView<CustomButton>(R.id.task_btn_goto).apply {
            if(item.isAccomplish){
                isEnabled=false
                text="已完成"
            }else{
                isEnabled=true
                text="去完成"
                when(item.taskType){
                    TYPE_READ_CHAPTER->{
                        val count = BookTodayReadChapterCountDaoManager.getTodayReadChapterCount()
                        if(count>=item.norm) text = "领金币"
                    }
                    TYPE_READ_TIME->{
                        val readTime = BookTodayReadTimeDaoManager.getTodayReadTime()
                        if(readTime>=item.norm*60) text = "领金币"
                    }
                    TYPE_WRITE_COMMENT->{
                        val count = BookTodayCommentWriteCountDaoManager.getTodayCommentWriteCount()
                        if(count>=item.norm) text = "领金币"
                    }
                    TYPE_WATCH_REWARD_ADVERT->{
                        val count = TodayWatchRewardAdvertCountDaoManager.getTodayWatchAdvertCount()
                        if(count>=item.norm) text = "领金币"
                    }
                }
            }
        }

    }

}